home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / A-B / 001. Sample.cpt / Sample.p < prev    next >
Text File  |  1988-08-02  |  23KB  |  705 lines

  1. {------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    MultiFinder-Aware Simple Sample Application
  6. #
  7. #    Sample
  8. #
  9. #    Sample.p    -    Pascal Source
  10. #
  11. #    Copyright © 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #
  16. #    Components:    Sample.p            August 1, 1988
  17. #                Sample.c            August 1, 1988
  18. #                Sample.r            August 1, 1988
  19. #                Sample.h            August 1, 1988
  20. #                PSample.make        August 1, 1988
  21. #                CSample.make        August 1, 1988
  22. #
  23. #    Sample is an example application that demonstrates how to
  24. #    initialize the commonly used toolbox managers, operate 
  25. #    successfully under MultiFinder, handle desk accessories, 
  26. #    and create, grow, and zoom windows.
  27. #
  28. #    It does not by any means demonstrate all the techniques 
  29. #    you need for a large application. In particular, Sample 
  30. #    does not cover exception handling, multiple windows/documents, 
  31. #    sophisticated memory management, printing, or undo. All of 
  32. #    these are vital parts of a normal full-sized application.
  33. #
  34. #    This application is an example of the form of a Macintosh 
  35. #    application; it is NOT a template. It is NOT intended to be 
  36. #    used as a foundation for the next world-class, best-selling, 
  37. #    600K application. A stick figure drawing of the human body may 
  38. #    be a good example of the form for a painting, but that does not 
  39. #    mean it should be used as the basis for the next Mona Lisa.
  40. #
  41. #    We recommend that you review this program or TESample before 
  42. #    beginning a new application.
  43. #
  44. ------------------------------------------------------------------------------}
  45.  
  46.  
  47. PROGRAM Sample;
  48.  
  49.  
  50. {Segmentation strategy:
  51.  
  52.  This program consists of three segments. Main contains most of the code,
  53.  including the MPW libraries, and the main program. Initialize contains
  54.  code that is only used once, during startup, and can be unloaded after the
  55.  program starts. %A5Init is automatically created by the Linker to initialize
  56.  globals for the MPW libraries and is unloaded right away.}
  57.  
  58.  
  59. {SetPort strategy:
  60.  
  61.  Toolbox routines do not change the current port. In spite of this, in this
  62.  program we use a strategy of calling SetPort whenever we want to draw or
  63.  make calls which depend on the current port. This makes us less vulnerable
  64.  to bugs in other software which might alter the current port (such as the
  65.  bug (feature?) in many desk accessories which change the port on OpenDeskAcc).
  66.  Hopefully, this also makes the routines from this program more self-contained,
  67.  since they don't depend on the current port setting.}
  68.  
  69.  
  70. USES
  71.     MemTypes, QuickDraw, OSIntf, ToolIntf;
  72.  
  73. CONST
  74.     {MPW 3.0 will include a Traps.p interface file that includes constants for trap numbers.
  75.      These constants are from that file.}
  76.     _WaitNextEvent            = $A860;
  77.     _Unimplemented            = $A89F;
  78.  
  79.     {SysEnvironsVersion is passed to SysEnvirons to tell it which version of the
  80.      SysEnvRec we understand.}
  81.     sysEnvironsVersion        = 1;
  82.  
  83.     {OSEvent is the event number of the suspend/resume and mouse-moved events sent
  84.      by MultiFinder. Once we determine that an event is an osEvent, we look at the
  85.      high byte of the message sent to determine which kind it is. To differentiate
  86.      suspend and resume events we check the resumeMask bit.}
  87.     osEvent                    = app4Evt;    {event used by MultiFinder}
  88.     suspendResumeMessage    = 1;        {high byte of suspend/resume event message}
  89.     resumeMask                = 1;        {bit of message field for resume vs. suspend}
  90.     mouseMovedMessage        = $FA;        {high byte of mouse-moved event message}
  91.  
  92.     {This is the minimum size (in K) for the application to run.}
  93.     kMinSize    = 22;
  94.     
  95.     {ExtremeNeg and ExtremePos are used to set up wide open rectangles and regions.}
  96.     extremeNeg    = -32768;
  97.     extremePos    = 32767 - 1;            {required for old region bug}
  98.     
  99.     {The following constants are all resource IDs, corresponding to resources in Sample.r.}
  100.     rMenuBar    = 128;                    {application's menu bar}
  101.     rAboutAlert    = 128;                    {about alert}
  102.     rWindow        = 128;                    {application's window}
  103.     rStopRect    = 128;                    {rectangle for Stop light}
  104.     rGoRect        = 129;                    {rectangle for Go light}
  105.  
  106.     {The following constants are used to identify menus and their items. The menu IDs
  107.      have an "m" prefix and the item numbers within each menu have an "i" prefix.}
  108.     mApple        = 128;                    {Apple menu}
  109.     iAbout        = 1;
  110.  
  111.     mFile        = 129;                    {File menu}
  112.     iNew        = 1;
  113.     iClose        = 4;
  114.     iQuit        = 12;
  115.  
  116.     mEdit        = 130;                    {Edit menu}
  117.     iUndo        = 1;
  118.     iCut        = 3;
  119.     iCopy        = 4;
  120.     iPaste        = 5;
  121.     iClear        = 6;
  122.  
  123.     mLight        = 131;                    {Light menu}
  124.     iStop        = 1;
  125.     iGo            = 2;
  126.  
  127.  
  128. VAR
  129.     {The "g" prefix is used to emphasize that a variable is global.}
  130.  
  131.     {GMac is used to hold the result of a SysEnvirons call. This makes
  132.      it convenient for any routine to check the environment.}
  133.     gMac                : SysEnvRec;    {set up by Initialize}
  134.  
  135.     {GHasWaitNextEvent is set at startup, and tells whether the WaitNextEvent
  136.      trap is available. If it is false, we know that we must call GetNextEvent.}
  137.     gHasWaitNextEvent    : BOOLEAN;        {set up by Initialize}
  138.  
  139.     {GInBackground is maintained by our osEvent handling routines. Any part of
  140.      the program can check it to find out if it is currently in the background.}
  141.     gInBackground        : BOOLEAN;        {maintained by Initialize and DoEvent}
  142.  
  143.     {GGrowRect is a rectangle representing the grow/shrink limits for GrowWindow.}
  144.     gGrowRect            : Rect;            {set up by Initialize}
  145.  
  146.  
  147.     {The following globals are the state of the window. If we supported more than
  148.      one window, they would be attatched to each document, rather than globals.}
  149.  
  150.     {GStopped tells whether the stop light is currently on stop or go.}
  151.     gStopped            : BOOLEAN;        {maintained by Initialize and SetLight}
  152.  
  153.     {GStopRect and gGoRect are the rectangles of the two stop lights in the window.}
  154.     gStopRect            : Rect;            {set up by Initialize}
  155.     gGoRect                : Rect;            {set up by Initialize}
  156.  
  157.  
  158. {$S Initialize}
  159. FUNCTION TrapAvailable(tNumber: INTEGER; tType: TrapType): BOOLEAN;
  160.  
  161. {Check to see if a given trap is implemented. This is only used by the
  162.  Initialize routine in this program, so we put it in the Initialize segment.
  163.  The recommended approach to see if a trap is implemented is to see if
  164.  the address of the trap routine is the same as the address of the
  165.  Unimplemented trap.}
  166.  
  167. BEGIN
  168.     {Check and see if the trap exists. On 64K ROM machines, tType will be ignored.}
  169.  
  170.     TrapAvailable := NGetTrapAddress(tNumber, tType) <> GetTrapAddress(_Unimplemented);
  171. END; {TrapAvailable}
  172.  
  173.  
  174. {$S Main}
  175. FUNCTION IsDAWindow(window: WindowPtr): BOOLEAN;
  176.  
  177. {Check if a window belongs to a desk accessory.}
  178.  
  179. BEGIN
  180.     IF window = NIL THEN
  181.         IsDAWindow := FALSE
  182.     ELSE    {DA windows have negative windowKinds}
  183.         IsDAWindow := WindowPeek(window)^.windowKind < 0;
  184. END; {IsDAWindow}
  185.  
  186.  
  187. {$S Main}
  188. FUNCTION IsAppWindow(window: WindowPtr): BOOLEAN;
  189.  
  190. {Check to see if a window belongs to the application. If the window pointer
  191.  passed was NIL, then it could not be an application window. WindowKinds
  192.  that are negative belong to the system and windowKinds less than userKind
  193.  are reserved by Apple except for windowKinds equal to dialogKind, which
  194.  mean it is a dialog.}
  195.  
  196. BEGIN
  197.     IF window = NIL THEN
  198.         IsAppWindow := FALSE
  199.     ELSE    {application windows have windowKinds >= userKind (8) or dialogKind (2)}
  200.         WITH WindowPeek(window)^ DO
  201.             IsAppWindow := (windowKind >= userKind) | (windowKind = dialogKind);
  202. END; {IsAppWindow}
  203.  
  204.  
  205. {$S Main}
  206. PROCEDURE DoCloseWindow(window: WindowPtr);
  207.  
  208. {Close a window. This handles only desk accessory windows because we do not
  209.  allow our window to be closed. TESample provides an example of how to handle
  210.  the closing of application windows.}
  211.  
  212. BEGIN
  213.     IF IsDAWindow(window) THEN
  214.         CloseDeskAcc(WindowPeek(window)^.windowKind);
  215. END; {DoCloseWindow}
  216.  
  217.  
  218. {$S Main}
  219. PROCEDURE ForceEnvirons;
  220.  
  221. {Make sure that the machine has at least 128K ROMs and enough memory to run.
  222.  If it doesn't, exit. SysEnvirons can be called before the toolbox managers
  223.  are initialized, and we need to call it at this point so we can check for
  224.  the right ROMs and memory availability before we call MaxApplZone and
  225.  initialize the toolbox managers. If AppleTalk has not been initialized, you
  226.  won't get the version of the AppleTalk driver that is running. That is not
  227.  critical here, and if that information was required, SysEnvirons could be
  228.  called again after AppleTalk had been initialized.}
  229.  
  230. VAR
  231.     ignoreError    : OSErr;
  232. BEGIN
  233.     {ignore the error returned from SysEnvirons; even if an error occurred,
  234.      the SysEnvirons glue will fill in the SysEnvRec}
  235.     ignoreError := SysEnvirons(sysEnvironsVersion, gMac);
  236.     IF (gMac.machineType < 0) |
  237.         (StackSpace + ORD(GetApplLimit) - ORD(ApplicZone) < kMinSize * 1024) THEN
  238.         ExitToShell;
  239.     {if you have stack requirements that differ from the default,
  240.     then you could use SetApplLimit to increase StackSpace at this point.}
  241. END; {ForceEnvirons}
  242.  
  243.  
  244. {$S Initialize}
  245. PROCEDURE GoGetRect(rectID: INTEGER; VAR theRect: Rect);
  246.  
  247. {This utility loads the global rectangles that are used by the window
  248.  drawing routines. It shows how the resource manager can be used to hold
  249.  values in a convenient manner. These values are then easily altered without
  250.  having to re-compile the source code. In this particular case, we know
  251.  that this routine is being called at initialization time. Therefore,
  252.  if a failure occurs here, we will assume that the application is in such
  253.  bad shape that we should just exit. Your error handling may differ, but
  254.  the check should still be made.}
  255.  
  256. TYPE
  257.     RectPtr        = ^Rect;
  258.     RectHnd        = ^RectPtr;
  259. VAR
  260.     resource    : Handle;
  261. BEGIN
  262.     resource := GetResource('RECT', rectID);
  263.     IF resource = NIL THEN ExitToShell;
  264.     theRect := RectHnd(resource)^^;
  265. END; {GoGetRect}
  266.  
  267.  
  268. {$S Initialize}
  269. PROCEDURE Initialize;
  270.  
  271. {Set up the whole world, including global variables, Toolbox managers,
  272.  and menus. We also create our one application window at this time.
  273.  Since window storage is non-relocateable, how and when to allocate space
  274.  for windows is very important so that heap fragmentation does not occur.
  275.  Because Sample has only one window and it is only disposed when the application
  276.  quits, we will allocate its space here, before anything that might be a locked
  277.  relocatable object gets into the heap. This way, we can force its storage to be
  278.  in the lowest memory available in the heap. Window storage can differ widely
  279.  amongst applications depending on how many windows are created and disposed.
  280.  If a failure occurs here, we will consider that the application is in such
  281.  bad shape that we should just exit. Your error handling may differ, but
  282.  the checks should still be made.}
  283.  
  284. VAR
  285.     menuBar        : Handle;
  286.     window        : WindowPtr;
  287.  
  288. BEGIN
  289.     gHasWaitNextEvent := TrapAvailable(_WaitNextEvent, ToolTrap);
  290.     gInBackground := FALSE;
  291.  
  292.     InitGraf(@thePort);
  293.     InitFonts;
  294.     InitWindows;
  295.     InitMenus;
  296.     TEInit;
  297.     InitDialogs(NIL);
  298.     InitCursor;
  299.  
  300. {    we will allocate our own window storage instead of letting the Window
  301.     Manager do it because GetNewWindow may load in temp. resources before
  302.     making the NewPtr call, and this can lead to heap fragmentation. }
  303.     window := WindowPtr(NewPtr(SIZEOF(WindowRecord)));
  304.     IF window = NIL THEN ExitToShell;
  305.     window := GetNewWindow(rWindow, Ptr(window), WindowPtr(-1));
  306.  
  307.     menuBar := GetNewMBar(rMenuBar);        {read menus into menu bar}
  308.     IF menuBar = NIL THEN ExitToShell;
  309.     SetMenuBar(menuBar);                    {install menus}
  310.     DisposHandle(menuBar);
  311.     AddResMenu(GetMHandle(mApple), 'DRVR');    {add DA names to Apple menu}
  312.     DrawMenuBar;
  313.  
  314.     gStopped := TRUE;
  315.     GoGetRect(rStopRect, gStopRect);        {the stop light rectangle}
  316.     GoGetRect(rGoRect, gGoRect);            {the go light rectangle}
  317. END; {Initialize}
  318.  
  319.  
  320. {$S Main}
  321. PROCEDURE DoCloseBehind(window: WindowPtr);
  322.  
  323. {Close the window that is passed and all windows behind it.
  324.  This closes windows from back to front, by calling itself
  325.  recursively, which minimizes window updating. Always keep
  326.  in mind the dangers of stack overflow when recursive routines
  327.  are used in situations where the calling level gets too
  328.  deep. That is not a problem here.}
  329.  
  330. BEGIN
  331.     IF window <> NIL THEN BEGIN    {if we are passed a window, close other windows behind it first}
  332.         DoCloseBehind(WindowPtr(WindowPeek(window)^.nextWindow));
  333.         DoCloseWindow(window);    {now that all the windows behind are closed, close this one}
  334.     END;
  335. END; {DoCloseBehind}
  336.  
  337.  
  338. {$S Main}
  339. PROCEDURE Terminate;
  340.  
  341. {Clean up the application and exits. We close all of the windows so that
  342.  they can update their documents, if any.}
  343.  
  344. BEGIN
  345.     DoCloseBehind(FrontWindow);    {close all windows}
  346.     ExitToShell;
  347. END; {Terminate}
  348.  
  349.  
  350. {$S Main}
  351. PROCEDURE SetLight(window: WindowPtr; newStopped: BOOLEAN);
  352.  
  353. {Change the setting of the light.}
  354.  
  355. BEGIN
  356.     IF newStopped <> gStopped THEN BEGIN
  357.         gStopped := newStopped;
  358.         SetPort(window);
  359.         InvalRect(window^.portRect);
  360.     END;
  361. END; {SetLight}
  362.  
  363.  
  364. {$S Main}
  365. PROCEDURE AdjustMenus;
  366.  
  367. {Enable and disable menus based on the current state.
  368.  The user can only select enabled menu items. We set up all the menu items
  369.  before calling MenuSelect or MenuKey, since these are the only times that
  370.  a menu item can be selected. Note that MenuSelect is also the only time
  371.  the user will see menu items. This approach to deciding what enable/
  372.  disable state a menu item has the advantage of concentrating all the decision-
  373.  making in one routine, as opposed to being spread throughout the application.
  374.  Other application designs may take a different approach that is just as valid.}
  375.  
  376. VAR
  377.     window            : WindowPtr;
  378.     menu            : MenuHandle;
  379.  
  380. BEGIN
  381.     window := FrontWindow;
  382.  
  383.     menu := GetMHandle(mFile);
  384.     IF IsDAWindow(window) THEN                {we can allow desk accessories to be closed from the menu}
  385.         EnableItem(menu, iClose)
  386.     ELSE
  387.         DisableItem(menu, iClose);            {but not our traffic light window}
  388.  
  389.     menu := GetMHandle(mEdit);
  390.     IF IsDAWindow(window) THEN BEGIN        {a desk accessory might need the edit menu}
  391.         EnableItem(menu, iUndo);
  392.         EnableItem(menu, iCut);
  393.         EnableItem(menu, iCopy);
  394.         EnableItem(menu, iPaste);
  395.         EnableItem(menu, iClear);
  396.     END ELSE BEGIN                            {but we know we do not}
  397.         DisableItem(menu, iUndo);
  398.         DisableItem(menu, iCut);
  399.         DisableItem(menu, iCopy);
  400.         DisableItem(menu, iClear);
  401.         DisableItem(menu, iPaste);
  402.     END;
  403.  
  404.     menu := GetMHandle(mLight);
  405.     IF IsAppWindow(window) THEN BEGIN        {we know that it must be the traffic light}
  406.         EnableItem(menu, iStop);
  407.         EnableItem(menu, iGo);
  408.     END ELSE BEGIN
  409.         DisableItem(menu, iStop);
  410.         DisableItem(menu, iGo);
  411.     END;
  412.     CheckItem(menu, iStop, gStopped);        {we can also determine check/uncheck state, too}
  413.     CheckItem(menu, iGo, NOT gStopped);
  414. END; {AdjustMenus}
  415.  
  416.  
  417. {$S Main}
  418. PROCEDURE DoMenuCommand(menuResult: LONGINT);
  419.  
  420. {This is called when an item is chosen from the menu bar (after calling
  421.  MenuSelect or MenuKey). It performs the right operation for each command.
  422.  It is good to have both the result of MenuSelect and MenuKey go to
  423.  one routine like this to keep everything organized.}
  424.  
  425. VAR
  426.     menuID            : INTEGER;        {the resource ID of the selected menu}
  427.     menuItem        : INTEGER;        {the item number of the selected menu}
  428.     itemHit            : INTEGER;
  429.     daName            : Str255;
  430.     daRefNum        : INTEGER;
  431.     handledByDA        : BOOLEAN;
  432.  
  433. BEGIN
  434.     menuID := HiWrd(menuResult);    {use built-ins (for efficiency)...}
  435.     menuItem := LoWrd(menuResult);    {to get menu item number and menu number}
  436.     CASE menuID OF
  437.         mApple:
  438.             CASE menuItem OF
  439.                 iAbout:                {bring up alert for About}
  440.                     itemHit := Alert(rAboutAlert, NIL);
  441.                 OTHERWISE BEGIN        {all non-About items in this menu are DAs}
  442.                     GetItem(GetMHandle(mApple), menuItem, daName);
  443.                     daRefNum := OpenDeskAcc(daName);
  444.                 END;
  445.             END;
  446.         mFile:
  447.             CASE menuItem OF
  448.                 iClose:
  449.                     DoCloseWindow(FrontWindow);
  450.                 iQuit:
  451.                     Terminate;
  452.             END;
  453.         mEdit:                        {call SystemEdit for DA editing & MultiFinder}
  454.             handledByDA := SystemEdit(menuItem-1);    {since we don't do any editing}
  455.         mLight:
  456.             CASE menuItem OF
  457.                 iStop:
  458.                     SetLight(FrontWindow, TRUE);
  459.                 iGo:
  460.                     SetLight(FrontWindow, FALSE);
  461.             END;
  462.     END;
  463.     HiliteMenu(0);                    {unhighlight what MenuSelect (or MenuKey) hilited}
  464. END; {DoMenuCommand}
  465.  
  466.  
  467. {$S Main}
  468. PROCEDURE DrawWindow(window: WindowPtr);
  469.  
  470. {Draw the contents of the application window. We do some drawing in color, using
  471.  Classic QuickDraw's color capabilities. This will be black and white on old
  472.  machines, but color on color machines. At this point, the window's visRgn is
  473.  set to allow drawing only where it needs to be done.}
  474.  
  475. BEGIN
  476.     SetPort(window);
  477.  
  478.     EraseRect(window^.portRect);    {clear out any garbage that might be left behind}
  479.     IF gStopped THEN                {draw a red (or white) stop light}
  480.         ForeColor(redColor)
  481.     ELSE
  482.         ForeColor(whiteColor);
  483.     PaintOval(gStopRect);
  484.     ForeColor(blackColor);
  485.     FrameOval(gStopRect);
  486.     IF NOT gStopped THEN            {draw a green (or white) go light}
  487.         ForeColor(greenColor)
  488.     ELSE
  489.         ForeColor(whiteColor);
  490.     PaintOval(gGoRect);
  491.     ForeColor(blackColor);
  492.     FrameOval(gGoRect);
  493. END; {DrawWindow}
  494.  
  495.  
  496. {$S Main}
  497. PROCEDURE DoContentClick(window: WindowPtr; event: EventRecord);
  498.  
  499. {This is called when a mouse-down event occurs in the content of a window.
  500.  Other applications might want to call FindControl, TEClick, etc., to
  501.  further process the click.}
  502.  
  503. BEGIN
  504.     SetLight(window, NOT gStopped);
  505. END; {DoContentClick}
  506.  
  507.  
  508. {$S Main}
  509. PROCEDURE DoUpdate(window: WindowPtr);
  510.  
  511. {This is called when an update event is received for a window.
  512.  It calls DrawWindow to draw the contents of an application window.
  513.  As an effeciency measure that does not have to be followed, it
  514.  calls the drawing routine only if the visRgn is non-empty. This
  515.  will handle situations where calculations for drawing or drawing
  516.  itself is very time-consuming.}
  517.  
  518. BEGIN
  519.     IF IsAppWindow(window) THEN BEGIN
  520.         BeginUpdate(window);                    {this sets up the visRgn}
  521.         IF NOT EmptyRgn(window^.visRgn) THEN    {draw if updating needs to be done}
  522.             DrawWindow(window);
  523.         EndUpdate(window);
  524.     END;
  525. END; {DoUpdate}
  526.  
  527.  
  528. {$S Main}
  529. PROCEDURE DoActivate(window: WindowPtr; becomingActive: BOOLEAN);
  530.  
  531. {This is called when a window is activated or deactivated.
  532.  In Sample, the Window Manager's handling of activate and
  533.  deactivate events is sufficient. Other applications may have
  534.  TextEdit records, controls, lists, etc., to activate/deactivate.}
  535.  
  536. BEGIN
  537.     IF IsAppWindow(window) THEN
  538.         IF becomingActive THEN
  539.             {do whatever you need to at activation}
  540.         ELSE
  541.             {do whatever you need to at deactivation};
  542. END; {DoActivate}
  543.  
  544.  
  545. {$S Main}
  546. PROCEDURE AdjustCursor(mouse: Point; region: RgnHandle);
  547.  
  548. {Change the cursor's shape, depending on its position. This also calculates the region
  549.  where the current cursor resides (for WaitNextEvent). If the mouse is ever outside of
  550.  that region, an event would be generated, causing this routine to be called,
  551.  allowing us to change the region to the region the mouse is currently in. If
  552.  there is more to the event than just “the mouse moved”, we get called before the
  553.  event is processed to make sure the cursor is the right one. In any (ahem) event,
  554.  this is called again before we fall back into WNE.}
  555.  
  556. VAR
  557.     window                : WindowPtr;
  558.     arrowRgn            : RgnHandle;
  559.     plusRgn                : RgnHandle;
  560.     globalPortRect        : Rect;
  561.  
  562. BEGIN
  563.     window := FrontWindow;    {we only adjust the cursor when we are in front}
  564.     IF (NOT gInBackground) AND (NOT IsDAWindow(window)) THEN BEGIN
  565.         {calculate regions for different cursor shapes}
  566.         arrowRgn := NewRgn;
  567.         plusRgn := NewRgn;
  568.  
  569.         {start with a big, big rectangular region}
  570.         SetRectRgn(arrowRgn, extremeNeg, extremeNeg, extremePos, extremePos);
  571.  
  572.         {calculate plusRgn}
  573.         IF IsAppWindow(window) THEN BEGIN
  574.             SetPort(window);            {make a global version of the portRect}
  575.             SetOrigin(-window^.portBits.bounds.left, -window^.portBits.bounds.top);
  576.             globalPortRect := window^.portRect;
  577.             RectRgn(plusRgn, globalPortRect);
  578.             SectRgn(plusRgn, window^.visRgn, plusRgn);
  579.             SetOrigin(0, 0);
  580.         END;
  581.  
  582.         {subtract other regions from arrowRgn}
  583.         DiffRgn(arrowRgn, plusRgn, arrowRgn);
  584.  
  585.         {change the cursor and the region parameter}
  586.         IF PtInRgn(mouse, plusRgn) THEN BEGIN
  587.             SetCursor(GetCursor(plusCursor)^^);
  588.             CopyRgn(plusRgn, region);
  589.         END ELSE BEGIN
  590.             SetCursor(arrow);
  591.             CopyRgn(arrowRgn, region);
  592.         END;
  593.  
  594.         {get rid of our local regions}
  595.         DisposeRgn(arrowRgn);
  596.         DisposeRgn(plusRgn);
  597.     END;
  598. END; {AdjustCursor}
  599.  
  600.  
  601. {$S Main}
  602. PROCEDURE DoEvent(event: EventRecord);
  603.  
  604. {Do the right thing for an event. Determine what kind of event it is, and call
  605.  the appropriate routines.}
  606.  
  607. VAR
  608.     part    : INTEGER;
  609.     window    : WindowPtr;
  610.     hit        : BOOLEAN;
  611.     key        : CHAR;
  612.  
  613. BEGIN
  614.     CASE event.what OF
  615.         mouseDown: BEGIN
  616.             part := FindWindow(event.where, window);
  617.             CASE part OF
  618.                 inMenuBar: BEGIN            {process the menu command}
  619.                     AdjustMenus;
  620.                     DoMenuCommand(MenuSelect(event.where));
  621.                 END;
  622.                 inSysWindow:                {let the system handle the mouseDown}
  623.                     SystemClick(event, window);
  624.                 inContent:
  625.                     IF window <> FrontWindow THEN BEGIN
  626.                         SelectWindow(window);
  627.                         {DoEvent(event);}    {use this line for "do first click"}
  628.                     END ELSE
  629.                         DoContentClick(window, event);
  630.                 inDrag:                        {pass screenBits.bounds to get all gDevices}
  631.                     DragWindow(window, event.where, screenBits.bounds);
  632.                 inGrow:;
  633.                 inZoomIn, inZoomOut:;
  634.             END;
  635.         END;
  636.         keyDown, autoKey: BEGIN                {check for menukey equivalents}
  637.             key := CHR(BAnd(event.message, charCodeMask));
  638.             IF BAnd(event.modifiers, cmdKey) <> 0 THEN    {Command key down}
  639.                 IF event.what = keyDown THEN BEGIN
  640.                     AdjustMenus;            {enable/disable/check menu items properly}
  641.                     DoMenuCommand(MenuKey(key));
  642.                 END;
  643.         END;                                {call DoActivate with the window and...}
  644.         activateEvt:                        {TRUE for activate, FALSE for deactivate}
  645.             DoActivate(WindowPtr(event.message), BAnd(event.modifiers, activeFlag) <> 0);
  646.         updateEvt:                          {call DoUpdate with the window to update}
  647.             DoUpdate(WindowPtr(event.message));
  648.         osEvent:
  649.             CASE BSR(event.message, 24) OF    {high byte of message}
  650.                 suspendResumeMessage: BEGIN
  651.                     gInBackground := BAnd(event.message, resumeMask) = 0;
  652.                     DoActivate(FrontWindow, NOT gInBackground);
  653.                 END;
  654.             END;
  655.     END;
  656. END; {DoEvent}
  657.  
  658.  
  659. {$S Main}
  660. PROCEDURE EventLoop;
  661.  
  662. {Get events forever, and handle them by calling DoEvent.
  663.  Get the events by calling WaitNextEvent, if it's available, otherwise
  664.  by calling GetNextEvent. Also call AdjustCursor each time through the loop.}
  665.  
  666. VAR
  667.     cursorRgn    : RgnHandle;
  668.     gotEvent    : BOOLEAN;
  669.     event        : EventRecord;
  670.  
  671. BEGIN
  672.     cursorRgn := NewRgn;            {we’ll pass WNE an empty region the 1st time thru}
  673.     REPEAT
  674.         IF gHasWaitNextEvent THEN    {put us 'asleep' forever under MultiFinder}
  675.             gotEvent := WaitNextEvent(everyEvent, event, MAXLONGINT, cursorRgn)
  676.         ELSE BEGIN
  677.             SystemTask;                {must be called if using GetNextEvent}
  678.             gotEvent := GetNextEvent(everyEvent, event);
  679.         END;
  680.         IF gotEvent THEN BEGIN
  681.             AdjustCursor(event.where, cursorRgn); {make sure we have the right cursor}
  682.             DoEvent(event);
  683.         END;
  684.         AdjustCursor(event.where, cursorRgn);
  685.     UNTIL FALSE;                    {loop forever; we quit through an ExitToShell}
  686. END; {EventLoop}
  687.  
  688.  
  689. PROCEDURE _DataInit; EXTERNAL;
  690.  
  691. {This routine is part of the MPW runtime library. This external
  692.  reference to it is done so that we can unload its segment, %A5Init.}
  693.  
  694. {$S Main}
  695. BEGIN
  696.     UnloadSeg(@_DataInit);    {note that _DataInit must not be in Main!}
  697.     ForceEnvirons;            {check for some basic requirements; exits if not met}
  698.     MaxApplZone;            {expand the heap so code segments load at the top}
  699.  
  700.     Initialize;                {initialize the program}
  701.     UnloadSeg(@Initialize);    {note that Initialize must not be in Main!}
  702.  
  703.     EventLoop;                {call the main event loop}
  704. END.
  705.